#include <head.h>
void my_sleep(int seconds);
int main()
{
    FILE *file = fopen("time2.txt", "w+");
    if (file == NULL)
    {
        printf("无法打开文件\n");
        return 1;
    }
    while (1)
    {
        time_t rawtime;
        struct tm *timeinfo;
        char buffer[80];

        time(&rawtime);
        timeinfo = localtime(&rawtime);

        strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
        fprintf(file, "%s\n", buffer);
        fflush(file); // 确保立即写入文件
        // sleep(1);     // 等待1秒
        my_sleep(1);
    }
    fclose(file);
    return 0;
}

void my_sleep(int seconds)
{
    clock_t start = clock();
    // while (clock() - start < seconds * CLOCKS_PER_SEC);
    while (difftime(clock(), start) < seconds * CLOCKS_PER_SEC);
}